package com.jjy.tools.validation.impl;

import com.jjy.service.impl.UserServiceImpl;
import com.jjy.tools.validation.Validation;

import javax.servlet.http.HttpServletRequest;

public class LoginValidation implements Validation{
  private RegisterErrorMessage registerErrorMessage = new RegisterErrorMessage();

  @Override
  public boolean doValidation(HttpServletRequest request, UserServiceImpl userServiceImpl) {
    boolean isValid = true;
    String account = request.getParameter("account");
    String password = request.getParameter("password");
    isValid = validateAccount(account , registerErrorMessage , isValid);
    isValid = validatePassword(password , registerErrorMessage , isValid);
    return isValid;
  }

  @Override
  public boolean validateRegex(String regex, String str) {
    return false;
  }

  public static boolean validateAccount(String account , RegisterErrorMessage registerErrorMessage , boolean isValid){
    if(account == null){
      registerErrorMessage.putErrorMessageToJson("account" , NULL_MESSAGE);
      isValid = false;
    } else if ("".equals(account)){
      registerErrorMessage.putErrorMessageToJson("account" , REQUIRED_MESSAGE);
      isValid = false;
    } else if (account.length() > 30 || account.length() < 8){
      registerErrorMessage.putErrorMessageToJson("account" , FORMAT_MESSAGE + "8到30长度的字母或数字");
      isValid = false;
    }
    return isValid;
  }

  public static boolean validatePassword(String password , RegisterErrorMessage registerErrorMessage , boolean isValid){
    if(password == null){
      registerErrorMessage.putErrorMessageToJson("password" , NULL_MESSAGE);
      isValid = false;
    } else if ("".equals(password)){
      registerErrorMessage.putErrorMessageToJson("password" , REQUIRED_MESSAGE);
      isValid = false;
    } else if (password.length() > 20 || password.length() < 8){
      registerErrorMessage.putErrorMessageToJson("password" , FORMAT_MESSAGE + "8到20长度的字母或数字");
      isValid = false;
    }
    return isValid;
  }


  public RegisterErrorMessage getRegisterErrorMessage() {
    return registerErrorMessage;
  }

  public void setRegisterErrorMessage(RegisterErrorMessage registerErrorMessage) {
    this.registerErrorMessage = registerErrorMessage;
  }

}
